/*********************************************************************************
 *      Copyright:  (C) 2014 None
 *                  All rights reserved.
 *
 *       Filename:  inputpanel.cpp
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(06/06/2014)
 *         Author:  yanshifu <469718937@qq.com>
 *      ChangeLog:  1, Release initial version on "06/06/2014 07:14:30 AM"
 *                 
 ********************************************************************************/

#include "inputpanel.h"
#include "ui_inputpanel.h"


inputpanel::inputpanel(QWidget *parent):
    QWidget(parent),
    ui(new Ui::Form)

{
    ui->setupUi(this); 


    m_EditFee = 0.000;
    m_EditMoney = 0.000;
    m_EditPay = 0.000;

    Csocket();

    connect(ui->pushButton_0, SIGNAL(clicked()), this, SLOT(zeroClicked()));
    connect(ui->pushButton_1, SIGNAL(clicked()), this, SLOT(oneClicked()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(twoClicked()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(threeClicked()));
    connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(fourClicked()));
    connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(fiveClicked()));
    connect(ui->pushButton_6, SIGNAL(clicked()), this, SLOT(sixClicked()));
    connect(ui->pushButton_7, SIGNAL(clicked()), this, SLOT(sevenClicked()));
    connect(ui->pushButton_8, SIGNAL(clicked()), this, SLOT(eigthClicked()));
    connect(ui->pushButton_9, SIGNAL(clicked()), this, SLOT(nineClicked()));

    connect(ui->pushButton_10, SIGNAL(clicked()), this, SLOT(pointClicked()));
    connect(ui->pushButton_11, SIGNAL(clicked()), this, SLOT(clearClicked()));
    connect(ui->pushButton_12, SIGNAL(clicked()), this, SLOT(payClicked()));

}

inputpanel::~inputpanel()
{
    delete ui;
}


void inputpanel::zeroClicked()
{
    ui->display->setText(ui->display->text() + "0");
}

void inputpanel::oneClicked()
{
    ui->display->setText(ui->display->text() + "1");
}

void inputpanel::twoClicked()
{
    ui->display->setText(ui->display->text() + "2");
}

void inputpanel::threeClicked()
{
    ui->display->setText(ui->display->text() + "3");
}

void inputpanel::fourClicked()
{
    ui->display->setText(ui->display->text() + "4");
}

void inputpanel::fiveClicked()
{
    ui->display->setText(ui->display->text() + "5");
}

void inputpanel::sixClicked()
{
    ui->display->setText(ui->display->text() + "6");
}

void inputpanel::sevenClicked()
{
    ui->display->setText(ui->display->text() + "7");
}

void inputpanel::eigthClicked()
{
    ui->display->setText(ui->display->text() + "8");
}

void inputpanel::nineClicked()
{
    ui->display->setText(ui->display->text() + "9");
}

void inputpanel::pointClicked()
{
    ui->display->setText(ui->display->text() + ".");
}


void inputpanel::clearClicked()
{
    ui->display->clear();
}


void inputpanel::Csocket()
{

    clientsocket = new QTcpSocket(this);
    clientsocket->abort();
    clientsocket->connectToHost("127.0.0.1", 1234);


    connect(clientsocket, SIGNAL(readyRead()), this, SLOT(receive()));
    connect(clientsocket,SIGNAL(error(QAbstractSocket::SocketError)),
            this,SLOT(displayError(QAbstractSocket::SocketError)));


}


void inputpanel::receive()
{

    QByteArray data; 
    data = clientsocket->readAll();
    memcpy(&CardInfo, data, sizeof(CardInfo)); 


    displayname();

}


void inputpanel::displayname()
{

    if (CardInfo.senu[0] == 0xb5)
    {

        ui->namelabel->setText(QString(tr("姓名:%1").arg(tr("张三"))));
    }
    else
    {
        ui->namelabel->setText(QString(tr("姓名:%1").arg(tr("李四"))));
    }

    unsigned long long m_Money;

    m_Money = 0;

    for (int i=0; i<16; i++)

    {
        m_Money += (unsigned long long)(CardInfo.block_buf[i]*pow(10, i));
    }

    m_EditMoney =  (double)m_Money/1000.000;
    m_Money = 0;
    
    ui->label->setText(QString(tr("余额:%1").arg(m_EditMoney)));
}


void inputpanel::payClicked()
{
    int i = 0;

    unsigned char   buf[16];

    QString str = ui->display->text();
    double m_EditFee = str.toDouble();
    

    if (m_EditFee > m_EditMoney)
    {
        QMessageBox box;
        box.setText(tr("您的余额已不足"));
        box.exec();
        return;
    }

    unsigned long long payval;

    payval = (unsigned long long)((m_EditMoney - m_EditFee + 0.0005) * 1000);

    m_EditMoney= (double)payval / 1000.000;

    for(i = 0; i<16; i++)
    {
        CardInfo.block_buf[i] = 0;

        buf[i] = (unsigned char)(payval % 10);

        CardInfo.block_buf[i] = buf[i];

        payval = payval / 10;
    }

    CardInfo.flag = 2;

    clientsocket->write((char *)&CardInfo, sizeof(CardInfo));
   
    ui->label->setText(QString(tr("余额:%1").arg(m_EditMoney)));
    ui->display->clear();

    m_EditFee = 0;
    m_EditMoney = 0;
}

void inputpanel::displayError(QAbstractSocket::SocketError)
{
    qDebug() << clientsocket->errorString();
}


void inputpanel::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::warning(this, tr("退出提示"),tr("确认退出?"), QMessageBox::Cancel, QMessageBox::Ok);

    if (QMessageBox::Ok == ret)
    {
        CardInfo.flag = 3;

        clientsocket->write((char *)&CardInfo, sizeof(CardInfo));
        clientsocket->disconnectFromHost();
                                
        event->accept();
    }
    else
    {
        event->ignore();
    }
}
